package cn.ekuma.epos.datalogic.define.dao.crm;

import java.util.List;

import com.openbravo.bean.crm.viewbean.ProductCustomerView;
import com.openbravo.bean.infobean.CustomerInfo;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.PreparedSentence;
import com.openbravo.data.loader.SerializerWriteString;
import cn.ekuma.data.dao.ViewDAO;

public class ProductCustomerViewDAO extends ViewDAO<ProductCustomerView> {

	CustomerProductDAO customerProductDao;
	public static final String LIST_SQL="select A.ID,A.CUSTOMER_ID,A.PRODUCT_ID,A.PRICE_IN,A.TIME_IN,A.REASON_IN,A.PRICE_OUT,A.TIME_OUT,A.REASON_OUT,A.CONTRACTPRICE,B.TAXID,B.SEARCHKEY,B.NAME from CUSTOMER_PRODUCT A LEFT JOIN CUSTOMERS B ON A.CUSTOMER_ID = B.ID  where A.PRODUCT_ID=?";
	
	public ProductCustomerViewDAO(I_Session s,CustomerProductDAO customerProductDao) {
		super(s);
		this.customerProductDao=customerProductDao;
		
	}

	@Override
	public Class getSuportClass() {
		return ProductCustomerView.class;
	}

	@Override
	public ProductCustomerView readValues(DataRead dr, ProductCustomerView obj)
			throws BasicException {
		if(obj==null)
			obj=new ProductCustomerView();
		customerProductDao.readValues(dr, obj);
		CustomerInfo customerInfo=new CustomerInfo();
		customerInfo.setKey(obj.getCustomerId());
		int count=customerProductDao.getTableDefinition().getColumnCount();
		customerInfo.setTaxid(dr.getString(count+1));
		customerInfo.setSearchkey(dr.getString(count+2));
		customerInfo.setName(dr.getString(count+3));
		obj.setCustomerInfo(customerInfo);
		return obj;
	}
	
	public List<ProductCustomerView> listProductCustomerViewByProductId(String productId)throws BasicException {
		 return   new PreparedSentence(s,LIST_SQL, SerializerWriteString.INSTANCE, this).list(productId);
	}

}
